import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class NinjaM here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class NinjaM extends Enemigos
{
    private int Salud ;
    private int Speed;
    private int Tiempo;
    private int animationCounter;
    private boolean moveLeft;
    private final int NUM_IMAG = 4;
    private Elixir elixir;
    private Ninja ninja;
    private DagaNM dagaNM;
    private GreenfootSound DagaEncajada;
    private boolean isDead = false;
    
    private final GreenfootImage atacaLeft = new GreenfootImage("ninjaMLC.png");
    private final GreenfootImage atacaRight = new GreenfootImage(atacaLeft);
    
    private final GreenfootImage ninjaMLeft = new GreenfootImage("ninjamalo.png");
    private final GreenfootImage ninjaMRight = new GreenfootImage(ninjaMLeft);
    
    private final GreenfootImage[] RunLeft = new GreenfootImage[NUM_IMAG];//Arreglo de imagenes correr hacia la derecha
    private final GreenfootImage[] RunRight = new GreenfootImage[NUM_IMAG];
    public NinjaM(){
        Salud = 200; 
        Speed = 3;
        Tiempo = 0; 
        animationCounter = 0;
        isDead = false;
        for(int i = 0; i<NUM_IMAG; i++){
         RunLeft[i] = new GreenfootImage("ninjamaloC"+i+".png");
        }
        
        for(int i = 0; i<NUM_IMAG; i++){
         RunRight[i] = new GreenfootImage(RunLeft[i]);
         RunRight[i].mirrorHorizontally();
        }
        DagaEncajada = new GreenfootSound("DagaEncajada.wav");
        
        atacaRight.mirrorHorizontally();
        ninjaMRight.mirrorHorizontally();
    }
    
    /**
     * Act - do whatever the NinjaM wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(ninjaCerca()){
        
        if(moveLeft)
          setImage(atacaLeft);
          else
          setImage(atacaRight);
        
        if( Tiempo > 30){
             lanceKnife();
             Tiempo = 0;
            }
         Tiempo ++;
         
        if(Tiempo > 50)
          Tiempo = 0;
          
       }   
       
       if(moveLeft)
          setImage(ninjaMLeft);
          else
          setImage(ninjaMRight);
        
       checaColision();
    }  
    
   
       
    public boolean ninjaCerca(){
       Niveles mundo = (Niveles)this.getWorld();
       Ninja ninja = mundo.ninja();
       
       if( ninja.ninjaX() < this.getX()) 
           moveLeft = true;
           else
           moveLeft = false;
           
       if(this.getX() - 300 >= ninja.ninjaX()  || this.getX() + 300 <=  ninja.ninjaX())
            return false;
            else
            return true;
            
           
          }
     
       public void checaColision(){
       
        Niveles mundo = (Niveles)this.getWorld();
        ninja = mundo.ninja;
       
        if(isTouching(Daga.class)){
         DagaEncajada.play();
         removeTouching(Daga.class);
         Salud -= 50;
        
        }
        
        if(isTouching(Onda.class)){
         DagaEncajada.play();
         removeTouching(Onda.class);
          Salud -= 50;
            
       }
       
       if(Salud <= 0){
            ninja.aumentaPuntos(10);
            elixir = new Elixir();
            mundo.addObject(elixir, this.getX(), this.getY()+10);
            getWorld().removeObject(this);
            isDead = true; 
           }
 
          
     }
     
     public void lanceKnife(){
        
          World world = this.getWorld();
          Greenfoot.playSound("Cuchillo.wav");
          dagaNM = new DagaNM(moveLeft);
          world.addObject(dagaNM, this.getX(), this.getY()-50);
             
        }  
}
